package coms.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import coms.datatransfer.FileItemDTO;
import coms.datatransfer.ReviewDTO;
import coms.datatransfer.TransactionDTO;
import coms.datatransfer.UserDTO;
import coms.ejb.sessionbean.ConferenceItemBeanRemote;
import coms.ejb.sessionbean.FileBeanRemote;
import coms.ejb.sessionbean.PaymentBeanRemote;
import coms.ejb.sessionbean.ReviewBeanRemote;
import coms.service.BusinessDelegate;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

public class PaymentAction extends ActionSupport implements ModelDriven<TransactionDTO>{
	
	private static final long serialVersionUID = 1L;
	
	BusinessDelegate delegate;
	PaymentBeanRemote paymentBean;
	
	private TransactionDTO transaction= new TransactionDTO();
	
	private int newtransactionid;
	
	
	public int getNewtransactionid() {
		return newtransactionid;
	}




	public void setNewtransactionid(int newtransactionid) {
		this.newtransactionid = newtransactionid;
	}




	public String execute()
	
	{
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		Map<String,Object> session = ActionContext.getContext().getSession();
		
		
		if(request.getParameter("conferenceItemID")!=null)
		{
			
			session.put("bookedconferenceitemid",request.getParameter("conferenceItemID"));
			
		}
		
		
		return "SUCCESS";
	}
	
	
	

	public TransactionDTO getTransaction() {
		return transaction;
	}

	public void setTransaction(TransactionDTO transaction) {
		this.transaction = transaction;
	}

	
	public PaymentAction(){
		delegate = new BusinessDelegate();
		paymentBean = (PaymentBeanRemote)delegate.getBean("PaymentBean");
		paymentBean.initialize();
		
	}

	@Override
	public TransactionDTO getModel() {
		// TODO Auto-generated method stub
		return transaction;
	}
	
	
	
	public String paymentMethod(){
		
		try{
		
		Map<String,Object> session = ActionContext.getContext().getSession();
		
		if(session!=null)
		{
			transaction.setUserID(Integer.parseInt(session.get("userID").toString()));
			transaction.setConferenceItemID(Integer.parseInt(session.get("bookedconferenceitemid").toString()));
		}
		transaction.setPaymentDate(new Date());
		transaction.setPaymentStatus("success");		
		newtransactionid=paymentBean.makePayment(transaction).getTransactionID();
		
		System.out.println("Transaction:"+transaction.getTransactionID());
		
		
		
		return "SUCCESS";
		} catch (Exception e){
			return "exception";
		}			
		
		
		
	}
	
	
	
	
	
	
	
	
}
